package com.ykg.service.impl;

import com.ykg.dto.StowBeltDTO;
import com.ykg.mapper.BeltMapper;
import com.ykg.mapper.EntrustMapper;
import com.ykg.mapper.StampMapper;
import com.ykg.pojo.Belt;
import com.ykg.pojo.Entrust;
import com.ykg.pojo.UnloadInfo;
import com.ykg.service.IDTOService;
import com.ykg.service.IStowBeltService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类(接口)：
 * Created on 2021/6/25 13:52.
 * Author: zetong
 * Description:
 */
@Service
public class StowBeltServiceImpl implements IStowBeltService {

    @Autowired
    private EntrustMapper entrustMapper;
    @Autowired
    private IDTOService idtoService;
    @Autowired
    private StampMapper stampMapper;
    @Autowired
    private BeltMapper beltMapper;


    @Override//跑跺的委托单列表
    public List<Entrust> getEntrustStow(){
        List<Entrust> entrusts = new ArrayList<Entrust>();
        entrusts.addAll(entrustMapper.getEntrustByState(4));
        entrusts.addAll(entrustMapper.getEntrustByState(5));
        entrusts.addAll(entrustMapper.getEntrustByState(6));

        return entrusts;
    }
    @Override//待分配皮带委托单的详细信息
    public StowBeltDTO getEntrustNotStowDetailed(String entrustId){
        Entrust entrust = entrustMapper.findByEntrustId(entrustId);
        StowBeltDTO stowBeltDTO = idtoService.createStowBeltDTO(entrust);
        return stowBeltDTO;
    }
    @Override//分配皮带
    public StowBeltDTO startStowBelt(String entrustId){
        entrustMapper.updateState(entrustId,5);
        Entrust entrust = entrustMapper.findByEntrustId(entrustId);
        StowBeltDTO stowBeltDTO = idtoService.createStowBeltDTO(entrust);
        List<Belt> belts = beltMapper.findAll();//8个皮带的信息
        List<Belt> stowBelts = new ArrayList<Belt>();//分配给该委托单的皮带
        int needBelt = stowBeltDTO.getUnloadInfos().size();
        if(stowBeltDTO.getFreeBelts()>needBelt){
            for(int i = 0;i<8;i++){
                if(needBelt!=0){
                    if(belts.get(i).getState() == 0){
                        beltMapper.updateStateById(Long.valueOf(i+1),1,entrustId);
                        Belt belt = new Belt(Long.valueOf(i+1),1,entrustId);
                        stowBelts.add(belt);
                        needBelt--;
                    }else {
                        continue;
                    }
                }else {
                    stowBeltDTO.setFreeBelts(stowBeltDTO.getFreeBelts()-stowBeltDTO.getUnloadInfos().size());
                    stowBeltDTO.setBusyBelts(stowBeltDTO.getBusyBelts()+stowBeltDTO.getUnloadInfos().size());
                    stowBeltDTO.setStowState(1);
                    stowBeltDTO.setBelts(stowBelts);
                    break;
                }
            }
        }

        return stowBeltDTO;
    }
    @Override//完成皮带
    public int finishStowBelt(String entrustId){
        int result = entrustMapper.updateState(entrustId,6);
        return result;
    }
}
